﻿
using System;
using System.Collections.Generic;

using System.Text;
using Google.GData.Calendar;
using Google.GData.Extensions;
using System.Web;

namespace DGZfP.EventPublisher.Calendar
{
    public class EventByGoogleCalendarEvent : IEvent
    {

        private EventEntry entry;
        private bool isDirty;

        public bool IsDirty
        {
            get { return isDirty; }
            set { isDirty = value; }
        }

        public bool Synchronize(IEvent TemplateEvent)
        {
            ID = TemplateEvent.ID;
            Title = HttpUtility.HtmlEncode(TemplateEvent.Title);
            Location = TemplateEvent.Location;
            Content = HttpUtility.HtmlEncode(TemplateEvent.Content);
            ContextId = TemplateEvent.ContextId;
            AllDay = TemplateEvent.AllDay;
            StartDate = TemplateEvent.StartDate;
            EndDate = TemplateEvent.EndDate;
            return isDirty;
        }

        public EventEntry EventEntry
        {
            get { return entry; }
            set
            {
                entry = value;
                isDirty = false;
            }
        }

        public EventByGoogleCalendarEvent()
        {
            EventEntry = new EventEntry();
        }

        public EventByGoogleCalendarEvent(EventEntry Entry)
        {
            this.EventEntry = Entry;
        }

        #region Private methods

        private ExtendedProperty GetProperty(string local_name)
        {
            string ns = "http://dotnetnuke/calendar";
            string name = string.Format("{0}#{1}", ns, local_name);

            foreach (object o in EventEntry.ExtensionElements)
            {


                if (o is ExtendedProperty)
                {
                    var p = (ExtendedProperty)o;
                    if (p.Name == name)
                    {
                        return p;
                    }
                }
            }
            var prop = new ExtendedProperty();
            prop.Name = name;
            EventEntry.ExtensionElements.Add(prop);
            return prop;
        }
        #endregion

        #region IEvent Member

        public string ID
        {
            get
            {
                return GetProperty("id").Value;
                    
            }
            set
            {
                if (value != ID)
                {
                    ExtendedProperty p = GetProperty("id");
                    p.Value = value;
                    isDirty = true;
                }
            }
        }



        public string Title
        {
            get
            {
                return EventEntry.Title.Text;
            }
            set
            {
                if (value != Title)
                {
                    EventEntry.Title.Text = value;
                    isDirty = true;
                }
            }
        }

        public string Content
        {
            get
            {
                return EventEntry.Content.Content;
            }
            set
            {
                if (value != Content)
                {
                    EventEntry.Content.Content = value;
                    isDirty = true;
                }
            }
        }

        public string Location
        {
            get
            {
                if (EventEntry.Locations.Count > 0)
                {
                    Where where = EventEntry.Locations[0];
                    return where.ValueString;
                }
                else return string.Empty;
            }
            set
            {
                if (value != Location)
                {
                    if (EventEntry.Locations.Count > 0)
                    {
                        Where where = EventEntry.Locations[0];
                        where.ValueString = value;
                    }
                    else EventEntry.Locations.Add(new Where("", "", value));
                    isDirty = true;
                }
            }
        }

        public DateTime? StartDate
        {
            get
            {
                if (EventEntry.Times.Count > 0)
                {
                    When when = EventEntry.Times[0];
                    return when.StartTime;
                }
                return null;
            }
            set
            {
                if (value != StartDate)
                {
                    if (value.HasValue)
                    {
                        if (EventEntry.Times.Count > 0)
                        {
                            When when = EventEntry.Times[0];
                            when.StartTime = value.Value;
                        }
                        else
                        {
                            var when = new When();
                            when.StartTime = value.Value;
                            EventEntry.Times.Add(when);
                        }
                    }
                    isDirty = true;
                }
            }
        }

        public DateTime? EndDate
        {
            get
            {
                if (EventEntry.Times.Count > 0)
                {
                    When when = EventEntry.Times[0];
                    return when.EndTime;
                }
                return null;
            }
            set
            {
                if (value != EndDate)
                {
                    if (value.HasValue)
                    {
                        if (EventEntry.Times.Count > 0)
                        {
                            When when = EventEntry.Times[0];
                            when.EndTime = value.Value;
                        }
                        else
                        {
                            var when = new When();
                            when.EndTime = value.Value;
                            EventEntry.Times.Add(when);
                        }
                    }
                    isDirty = true;
                }
            }
        }

        public Boolean AllDay
        {
            get
            {
                if (EventEntry.Times.Count > 0)
                {
                    When when = EventEntry.Times[0];
                    return when.AllDay;
                }
                else return false;
            }
            set
            {
                if (value != AllDay)
                {
                    if (EventEntry.Times.Count > 0)
                    {
                        When when = EventEntry.Times[0];
                        when.AllDay = value;
                    }
                    else
                    {
                        var when = new When();
                        when.AllDay = value;
                        EventEntry.Times.Add(when);
                    }
                    isDirty = true;
                }
            }
        }
        public string ContextId
        {
            get
            {
                return GetProperty("ContextId").Value;
            }
            set
            {
                if (value != ContextId)
                {
                    GetProperty("ContextId").Value = value;
                    isDirty = true;
                }
            }
        }


        #endregion


    }
}
